package api

import (
	"go-weiqi-system/config"
	"go-weiqi-system/internal/models"
	"go-weiqi-system/internal/utils"

	"github.com/gin-gonic/gin"
)

type NotificationController struct{}

// Index 通知列表
func (ctrl *NotificationController) Index(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var param models.PageParam
	if err := c.ShouldBindQuery(&param); err != nil {
		param.Page = 1
		param.PageSize = 10
	}

	isRead := c.Query("is_read")

	var notifications []models.Notification
	var total int64

	db := config.DB.Model(&models.Notification{}).Where("user_id = ?", userID)

	if isRead != "" {
		db = db.Where("is_read = ?", isRead)
	}

	db.Count(&total)
	db.Order("id desc").
		Offset(param.GetOffset()).
		Limit(param.GetPageSize()).
		Find(&notifications)

	utils.Success(c, models.PageResponse{
		List:     notifications,
		Total:    total,
		Page:     param.GetPage(),
		PageSize: param.GetPageSize(),
	})
}

// Detail 通知详情
func (ctrl *NotificationController) Detail(c *gin.Context) {
	userID, _ := c.Get("user_id")
	id := c.Query("id")

	if id == "" {
		utils.Error(c, "参数错误")
		return
	}

	var notification models.Notification
	if err := config.DB.Where("id = ? AND user_id = ?", id, userID).
		First(&notification).Error; err != nil {
		utils.Error(c, "通知不存在")
		return
	}

	// 标记为已读
	if notification.IsRead == 0 {
		config.DB.Model(&notification).Update("is_read", 1)
		notification.IsRead = 1
	}

	utils.Success(c, notification)
}

// MarkRead 标记为已读
func (ctrl *NotificationController) MarkRead(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var req struct {
		ID uint `json:"id" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	if err := config.DB.Model(&models.Notification{}).
		Where("id = ? AND user_id = ?", req.ID, userID).
		Update("is_read", 1).Error; err != nil {
		utils.Error(c, "操作失败")
		return
	}

	utils.Success(c, nil, "操作成功")
}

// MarkAllRead 全部标记为已读
func (ctrl *NotificationController) MarkAllRead(c *gin.Context) {
	userID, _ := c.Get("user_id")

	if err := config.DB.Model(&models.Notification{}).
		Where("user_id = ? AND is_read = ?", userID, 0).
		Update("is_read", 1).Error; err != nil {
		utils.Error(c, "操作失败")
		return
	}

	utils.Success(c, nil, "操作成功")
}

// UnreadCount 未读数量
func (ctrl *NotificationController) UnreadCount(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var count int64
	config.DB.Model(&models.Notification{}).
		Where("user_id = ? AND is_read = ?", userID, 0).
		Count(&count)

	utils.Success(c, gin.H{
		"count": count,
	})
}

// Delete 删除通知
func (ctrl *NotificationController) Delete(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var req struct {
		ID uint `json:"id" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	if err := config.DB.Where("id = ? AND user_id = ?", req.ID, userID).
		Delete(&models.Notification{}).Error; err != nil {
		utils.Error(c, "删除失败")
		return
	}

	utils.Success(c, nil, "删除成功")
}
